package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.mapper.OperationLogMapper;
import com.yupi.springbootinit.model.entity.OperationLog;
import com.yupi.springbootinit.service.OperationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 * 操作日志服务实现类
 */
@Service
@Slf4j
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {

    @Override
    public void recordLog(Long userId, String userName, String module, String operation, String content, String ipAddress) {
        try {
            OperationLog operationLog = new OperationLog();
            operationLog.setUserId(userId);
            operationLog.setUserName(userName);
            operationLog.setModule(module);
            operationLog.setOperation(operation);
            operationLog.setContent(content);
            operationLog.setIpAddress(ipAddress);
            operationLog.setCreateTime(new Date());
            
            this.save(operationLog);
        } catch (Exception e) {
            log.error("记录操作日志失败", e);
        }
    }

    @Override
    @Async("taskExecutor")
    public void recordLogAsync(Long userId, String userName, String module, String operation, String content, String ipAddress) {
        recordLog(userId, userName, module, operation, content, ipAddress);
    }
}